package com.kalix.jyksh.record.biz;

import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.framework.core.util.ConfigUtil;
import com.kalix.ksh.proint.api.model.InfUtil;
import com.kalixjyksh.record.api.biz.IWarningInfRecordBeanService;
import com.kalix.ksh.record.api.dao.IWarningInfRecordBeanDao;
import com.kalixjyksh.record.api.model.HttpRequestUtil;
import com.kalix.jyksh.record.entities.WarningInfRecordBean;
import org.json.JSONArray;
import org.json.JSONObject;

import java.text.DecimalFormat;


/**
 *
 * 功能描述: 根据告警信息扇形图需要数据格式，返回数据
 *
 * @auther: Whw
 * @date: 2019/1/14 0014 17:10
 * @param:
 * @return:
 */


public class WarningInfRecordServiceImpl extends GenericBizServiceImpl<IWarningInfRecordBeanDao, WarningInfRecordBean> implements IWarningInfRecordBeanService {
    public WarningInfRecordServiceImpl(){
        super.init(WarningInfRecordBean.class.getName());
    }
    public static String wsIp = (String) ConfigUtil.getConfigProp("FREEBOARD_IP","config.ksh.portconf");
    public static String wsPort =(String)ConfigUtil.getConfigProp("FREEBOARD_PORT","config.ksh.portconf");
    public String  warningInf() {

        String url = "http://"+wsIp+":"+wsPort+"/query/mo/AlarmEvent?where=(alarmStatus=2%20or%20alarmStatus=3)&order=alarmTime%20desc";
        JSONArray  data1= HttpRequestUtil.getXpath(url);
        int a = data1.length();// 告警数
//        System.out.println(" 告 警 数 "+a);
        int b = getPolice();//处理数
        int num = a + b;// 总数
        double num1 = num ;
        double b1 = b ;
        int unhandlenum = num - b ; // 未处理数
//        System.out.println(" 未 处 理 数 " + unhandlenum);
        double p = b1/num1;
        double p1 = p*100;

        DecimalFormat    df   = new DecimalFormat("######0.00");
        String p2 = df.format(p1);//处理率

        String network = getnetwork();
        String host = gethost();
        String database = getdatabase();
        String service = getservice();

        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("value",network);
        jsonObject1.put("name","网络告警");
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("value",host);
        jsonObject2.put("name","主机告警");
        JSONObject jsonObject3 = new JSONObject();
        jsonObject3.put("value",database);
        jsonObject3.put("name","数据库告警");
        JSONObject jsonObject4 = new JSONObject();
        jsonObject4.put("value",service);
        jsonObject4.put("name","标准应用告警");

        jsonArray.put(jsonObject1);
        jsonArray.put(jsonObject2);
        jsonArray.put(jsonObject3);
        jsonArray.put(jsonObject4);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("dealWith",b);//已处理告警
        jsonObject.put("untreated",unhandlenum);//未处理数
        jsonObject.put("processingRate",p2);//处理率
        jsonObject.put("data",jsonArray);


        return jsonObject.toString();
    }
    //以处理数量
    public static int  getPolice(){
        String url = "http://"+wsIp+":"+wsPort+"/query/mo/AlarmEvent?where=(alarmStatus=4)&order=alarmTime%20desc";
        JSONArray  data2= HttpRequestUtil.getXpath(url);
        int b = data2.length();
//        System.out.println(" 从 "+sTime+" 时 间 到 "+lTime+" 处 理 了 告 警 "+b+" 个 ");
        return b;
    }

    /**
     * 取网络设备告警数量
     * @return
     */
    public static String getnetwork(){
        String url = "http://"+wsIp+":"+wsPort+"/query/mo/AlarmEvent?select=count(*)%20as%20count&where=topMoCategory=%27network%27";
        String data= InfUtil.interfaceUtil(url,"");
        JSONArray jsonArray = new JSONArray(data);
        JSONObject job = jsonArray.getJSONObject(0);
        JSONArray s = (JSONArray) job.get("properties");
        JSONObject job2 = s.getJSONObject(0);
        JSONObject js = (JSONObject) job2.get("value");
        Object network = js.get("value");

        return network.toString();
    }

    /**
     * 主机告警数量
     * @return
     */
    public static String gethost(){
        String url = "http://"+wsIp+":"+wsPort+"/query/mo/AlarmEvent?select=count(*)%20as%20count&where=topMoCategory=%27host%27";
        String data= InfUtil.interfaceUtil(url,"");
        JSONArray jsonArray = new JSONArray(data);
        JSONObject job = jsonArray.getJSONObject(0);
        JSONArray s = (JSONArray) job.get("properties");
        JSONObject job2 = s.getJSONObject(0);
        JSONObject js = (JSONObject) job2.get("value");
        Object host = js.get("value");

        return host.toString();
    }

    /**
     * 数据库告警数量
     * @return
     */
    public static String getdatabase(){
        String url = "http://"+wsIp+":"+wsPort+"/query/mo/AlarmEvent?select=count(*)%20as%20count&where=topMoCategory=%27database%27";
        String data= InfUtil.interfaceUtil(url,"");
        JSONArray jsonArray = new JSONArray(data);
        JSONObject job = jsonArray.getJSONObject(0);
        JSONArray s = (JSONArray) job.get("properties");
        JSONObject job2 = s.getJSONObject(0);
        JSONObject js = (JSONObject) job2.get("value");
        Object database = js.get("value");

        return database.toString();
    }

    /**
     * 标准应用告警数量
     * @return
     */
    public static String getservice(){
        String url = "http://"+wsIp+":"+wsPort+"/query/mo/AlarmEvent?select=count(*)%20as%20count&where=topMoCategory=%27service%27";
        String data= InfUtil.interfaceUtil(url,"");
        JSONArray jsonArray = new JSONArray(data);
        JSONObject job = jsonArray.getJSONObject(0);
        JSONArray s = (JSONArray) job.get("properties");
        JSONObject job2 = s.getJSONObject(0);
        JSONObject js = (JSONObject) job2.get("value");
        Object service = js.get("value");

        return service.toString();
    }
}
